<template>
  <div>
    <van-tabs v-model="active" sticky>
      <van-tab title="主体信息">
        <div class="page_space">
          <p class="title_1">基本信息</p>
          <base-card v-if="baseInfo.length > 0" :data="baseInfo" />

          <!-- <p class="title_1">证件信息</p>
          <input-card>
            <template v-if="subject.subjectType === 'FixedPlace'">
              {{ subject.businessFileDTOList }}
              <input-item-upload title="资质文件" tip="支持上传 png，jpg，jpeg">
                <file-upload
                  :imgId="item.fileId"
                  :maxLen="1"
                  deletable
                  @change="uploadFile($event, index)"
                />
              </input-item-upload>
            </template>
            <template
              v-else-if="subject.subjectType === 'TemporaryOperatingArea'"
            >
            {{ subject.businessFileDTOList }}
            </template>
            <template
              v-else-if="
                subject.subjectType === 'OtherSellersOfEdibleAgricultural'
              "
            >
            {{ subject.businessFileDTOList }}
            </template>
          </input-card> -->

          <p class="title_1">关联档口</p>
          <base-card
            v-for="(item, index) in relationStall"
            :key="index"
            :data="item.data"
            :title="item.title"
            emptyStr="暂无信息"
            class="mb-15"
          />
          <card v-if="relationStall.length === 0">
            <p class="text-center empty">暂无信息</p>
          </card>
        </div>
        <div class="btn_wrap">
          <button class="btn" @click="toInspect">立即采样</button>
        </div>
      </van-tab>

      <van-tab title="快检记录">
        <van-search
          v-model="params.businessVarietyName"
          placeholder="请输入样品名称"
          @search="onSearch"
          @cancel="onCancel"
        />
        <div class="page_space">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <base-card
              v-for="(item, index) in records"
              :key="index"
              :data="item.data"
              :title="item.title"
              :collapse="true"
              emptyStr="待检测"
              class="mt-15"
            />
          </van-list>
        </div>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import baseCard from '@/components/base-card'
import card from '@/components/base-card/wrap'
import inputItemUpload from '@/components/input-card/item-upload'
import { getSubjectDetail, getInspectRecord } from '@/api/subject'
import fileUpload from '@/components/file-upload'
import inputCard from '@/components/input-card/card'

export default {
  name: 'subjectDetails',
  components: { baseCard, card, inputItemUpload, fileUpload, inputCard },
  data() {
    return {
      loading: false,
      finished: false, // 触底加载结束
      active: 0,
      subject: {},
      baseInfo: [],
      relationStall: [], // 关联档口
      records: [], // 快检记录
      search: '',
      params: {
        buildControls: 'BUILD_REPORT_INFO,BUILD_SAMPLE_SORT_INFO',
        businessVarietyName: '',
        intercourseEnterpriseId: '',
        limit: 10,
        page: 1,
        stateNotIn: 'DRAFT,CANCEL,DELETED'
      }
    }
  },
  created() {
    let subjectId = this.$route.query.id
    this.params.intercourseEnterpriseId = subjectId
    this.getInfo(subjectId)
  },
  methods: {
    // 触底加载
    onLoad() {
      this.getInspectRecord()
    },
    onSearch(val) {
      this.businessVarietyName = val.trim()
      this.getInspectRecord(true)
    },
    onCancel() {
      this.params.businessVarietyName = ''
      this.getList(true)
    },
    // 获取快检记录
    async getInspectRecord(isReset) {
      if (isReset) {
        this.records = []
        this.loading = true
        this.finished = false
        this.params.page = 1
      }
      const { code, data } = await getInspectRecord(this.params)
      if (code === '0') {
        data.content.forEach((item) => {
          let rows = []
          item.reportInfoDTOList.forEach((row) => {
            // 快检记录
            if (row.reportDetailInfoDTOList.length > 0) {
              rows.push({
                key: row.reportDetailInfoDTOList[0].sampleProjectInfoDTO.name,
                value: row.reportDetailInfoDTOList[0].singleResultText,
                danger:
                  row.reportDetailInfoDTOList[0].singleResultText === '阳性'
              })
            }
          })
          this.records.push({
            title: `${item.businessVarietyNames} ${item.objectModificationTime}`,
            id: item.id,
            data: rows
          })
        })
        // console.log(this.records)
        this.params.page++
        // 加载状态结束
        this.loading = false
        // 数据全部加载完成
        if (data.last) {
          this.finished = true
        }
      }
    },
    async getInfo(id) {
      const { code, data } = await getSubjectDetail({
        id,
        buildControls: 'BUILD_FAST_CHECK_ENT'
      })
      if (code === '0') {
        this.subject = data
        let subjectType = data.subjectType
        if (subjectType === 'FixedPlace') {
          // 主体信息
          this.baseInfo = [
            {
              key: '主体类型',
              value: data.subjectTypeText
            },
            {
              key: '主体名称',
              value: data.name
            },
            {
              key: '营业执照/统一社会信用代码/身份证',
              value: data.uscId
            },
            {
              key: '所属区划',
              value: data.allAreaName
            },
            {
              key: '经营状态',
              value: data.manageStateText
            },
            {
              key: '主体性质',
              value: data.subjectNatureText
            },
            {
              key: '负责人姓名',
              value: data.legalPerson
            },
            {
              key: '联系人姓名',
              value: data.contacts
            },
            {
              key: '联系方式',
              value: data.contactsTel
            },
            {
              key: '经营范围',
              value: data.varieties
            }
          ]
          if (data.managementEntNameAndShortName) {
            this.baseInfo.splice(4, 0, {
              key: '所属市场',
              value: data.managementEntNameAndShortName
            })
          }
        } else if (subjectType === 'TemporaryOperatingArea') {
          // 市场临时
          this.baseInfo = [
            {
              key: '主体类型',
              value: data.subjectTypeText
            },
            {
              key: '主体名称',
              value: data.name
            },
            {
              key: '营业执照/统一社会信用代码/身份证',
              value: data.uscId
            },
            {
              key: '所属区划',
              value: data.allAreaName
            },
            {
              key: '所属市场',
              value: data.managementEntNameAndShortName
            },
            {
              key: '经营状态',
              value: data.manageStateText
            },
            {
              key: '主体性质',
              value: data.subjectNatureText
            },
            {
              key: '负责人姓名',
              value: data.legalPerson
            },
            {
              key: '联系人姓名',
              value: data.contacts
            },
            {
              key: '联系方式',
              value: data.contactsTel
            },
            {
              key: '经营范围',
              value: data.varieties
            },
            {
              key: '是否建立入场销售者档案',
              value: data.hasEntryRecord === true ? '是' : '否'
            },
            {
              key: '销售者入场日期',
              value: data.foundationDate
            },
            {
              key: '住所',
              value: data.home
            },
            {
              key: '食品经营许可证/仅销售预包装食品备案',
              value: data.licenseNumber
            }
          ]
        } else if (subjectType === 'OtherSellersOfEdibleAgricultural') {
          let subSubjectTypeList = [
            {
              text: '餐馆',
              id: 'Restaurant'
            },
            {
              text: '集体用餐配送单位',
              id: 'DeliveryUnitOfGroupMeal'
            },
            {
              text: '中央厨房',
              id: 'CentralKitchen'
            },
            {
              text: '学校食堂',
              id: 'SchoolCanteen'
            },
            {
              text: '托幼机构食堂',
              id: 'KindergartenCanteen'
            },
            {
              text: '便利店',
              id: 'MiniMart'
            },
            {
              text: '食杂店',
              id: 'GroceryStore'
            },
            {
              text: '商场超市',
              id: 'Supermarket'
            },
            {
              text: '食用农产品专卖店',
              id: 'AgriculturalProductStore'
            },
            {
              text: '其他',
              id: 'Other'
            }
          ]
          let subSubjectTypeText = subSubjectTypeList.find(
            (i) => i.id === data.subSubjectType
          )
          // 市场外
          this.baseInfo = [
            {
              key: '主体类型',
              value: data.subjectTypeText
            },
            {
              key: '主体名称',
              value: data.name
            },
            {
              key: '营业执照/统一社会信用代码',
              value: data.uscId
            },
            {
              key: '经营地址',
              value: data.allAreaName
            },
            {
              key: '经营详细地址',
              value: data.address
            },
            {
              key: '经营状态',
              value: data.manageStateText
            },
            {
              key: '主体性质',
              value: data.subjectNatureText
            },
            {
              key: '经营者类型',
              value: subSubjectTypeText.text
            },
            {
              key: '法定代表人姓名',
              value: data.legalPerson
            },
            {
              key: '联系人姓名',
              value: data.contacts
            },
            {
              key: '联系人方式',
              value: data.contactsTel
            },
            {
              key: '经营项目',
              value: data.varieties
            },
            {
              key: '备注',
              value: data.description
            }
          ]
        }

        // 关联档口
        let { relationEntPlaceInfoDTOList } = data
        relationEntPlaceInfoDTOList.forEach((item) => {
          this.relationStall.push({
            title: item.name,
            data: [
              {
                key: '档口状态',
                value: item.businessStatusText
              },
              {
                key: '档口编号',
                value: item.code
              }
            ]
          })
        })
      }
    },
    // 立即采样
    toInspect() {
      this.$router.push({
        name: 'immediateSampling',
        query: {
          intercourseEnterpriseInfo: encodeURIComponent(
            JSON.stringify({
              intercourseEnterpriseId: this.subject.id, // 经营主体id
              intercourseEnterpriseName: this.subject.name, // 经营主体
              subjectType: this.subject.subjectType, // 主体类型
              subjectTypeText: this.subject.subjectTypeText, // 主体类型 中文
              managementEntName: this.subject.managementEntName // 所属市场
            })
          )
        }
      })
    }
  }
}
</script>

<style lang="scss">
.empty {
  color: #656c74;
}
</style>
